﻿class KandM {
	//-----------------------属性
	[Inspectable(defaultValue=)]
	public var MouseDown:Function;
	[Inspectable(defaultValue=)]
	public var MouseUp:Function;
	[Inspectable(defaultValue=)]
	public var MouseMove:Function;
	[Inspectable(defaultValue=)]
	public var MouseDoubleDown:Function;
	[Inspectable(defaultValue=)]
	public var KeyDown:Function;
	[Inspectable(defaultValue=)]
	public var KeyUp:Function;
	[Inspectable(defaultValue=300)]
	public var MDtime:Number;
	//双击限时
	[Inspectable(defaultValue=200)]
	public var KLtime:Number;
	//粘连键限时
	//------------ mouse 标志
	public var MDC:Boolean = new Boolean();
	//左双击
	public var MSC:Boolean = new Boolean();
	//左单击
	public var MU:Boolean = new Boolean();
	//鼠标放开
	public var MD:Boolean = new Boolean();
	//鼠标按下
	public var x:Number = new Number();
	//鼠标x位置
	public var y:Number = new Number();
	//鼠标y位置
	//------------ key 标志
	public var KD:Boolean = new Boolean();
	//键盘按下
	public var KU:Boolean = new Boolean();
	//键盘放开
	//----------------------------------  标志性开关
	private var _ifwork:Boolean = new Boolean();
	private var enable:Boolean = new Boolean();
	//------------------------- 参数
	private var KeventA:Array;
	//处理粘连键的函数数组
	private var MObj:Object;
	//监听器
	private var KObj:Object;
	//监听器
	private var Mt:Number;
	//上次按下mouse的时间
	private var Kt:Number;
	//上次按下key的时间
	//-------------------------  me
	private var _me:String = "KeyMouse";
	//模块名
	private var ___V:String = "";
	//版本及说明
	//-------------- 构造函数
	public function KandM(_MDtime:Number, _KLtime:Number) {
		if (_MDtime == undefined) {
			MDtime = 300;
		} else {
			MDtime = _MDtime;
		}
		if (_KLtime == undefined) {
			KLtime = 200;
		} else {
			KLtime = _KLtime;
		}
		//  调用初始化函数.
		init();
	}
	//--------------------------初始化
	private function init() {
		// -这里写初始化函数或内容代码。
		Mt = 0;
		Kt = 0;
		MObj = new Object();
		KObj = new Object();
		_ifwork = false;
		enable = true;
		MDC = false;
		MSC = false;
		MU = false;
		MD = false;
		KD = false;
		KU = false;
		KeventA = new Array();
		//-------------------------------
		trace(___V);
		//版本消息.
		say("初始化完成");
	}
	//------------------------ 消息函数
	private function say(msg:String) {
		trace(_me + ":" + msg);
	}
	//=================================================扩展功能函数
	private function MDfun(k) {
		//鼠标按下
		trace("鼠标按下" + k);
		var dt:Date = new Date();
		var nowMt:Number = dt.getTime();
		var Lt:Number = nowMt - Mt;
		if (Lt <= MDtime) {
			MDC = true;
			MSC = false;
			MDCfun();
		} else {
			MSC = true;
		}
		Mt = nowMt;
		if (MouseDown != undefined) {
			var f:Function = MouseDown;
			if (typeof (f) == "function") {
				f();
			} else {
				say("MouseDown不是有效的函数");
			}
		}
	}
	private function MUfun() {
		//鼠标放开
		//trace("鼠标放开");
		MDC = false;
		if (MouseUp != undefined) {
			var f:Function = MouseUp;
			if (typeof (f) == "function") {
				f();
			} else {
				say("MouseUp不是有效的函数");
			}
		}
	}
	private function MMfun() {
		//鼠标移动
		//trace("鼠标移动");
		if (MouseMove != undefined) {
			var f:Function = MouseMove;
			if (typeof (f) == "function") {
				f();
			} else {
				say("MouseMove不是有效的函数");
			}
		}
	}
	private function MDCfun() {
		//鼠标双击
		trace("鼠标双击");
		if (MouseDoubleDown != undefined) {
			var f:Function = MouseDoubleDown;
			if (typeof (f) == "function") {
				f();
			} else {
				say("MouseDoubleDown不是有效的函数");
			}
		}
	}
	private function KUfun() {
		//keyup
		//trace("keyup");
		if (KeyUp != undefined) {
			var f:Function = KeyUp;
			if (typeof (f) == "function") {
				f();
			} else {
				say("KeyUp不是有效的函数");
			}
		}
	}
	private function KDfun() {
		//keydown
		//trace("keydown");
		if (KeyDown != undefined) {
			var f:Function = KeyDown;
			if (typeof (f) == "function") {
				f();
			} else {
				say("KeyDown不是有效的函数");
			}
		}
	}
	private function KLfun() {
		//粘连键动作
	}
	//===============================================系统功能函数
	public function get ifwork():Boolean {
		return _ifwork;
	}
	public function set ifwork(w:Boolean) {
		if (w == undefined) {
			return;
		}
		if (w) {
			startMe();
		} else {
			stopMe();
		}
	}
	private function startMe() {
		//起动监听
		var mcObj = this;
		if (!_ifwork) {
			//--------------------------- mouse
			MObj.onMouseDown = function() {
				mcObj.MDfun(mcObj._ifwork);
			};
			MObj.onMouseUp = MUfun;
			MObj.onMouseMove = MMfun;
			Mouse.addListener(MObj);
			//--------------------------  key
			KObj.onKeyDown = KDfun;
			KObj.onKeyUp = KUfun;
			Key.addListener(KObj);
			_ifwork = true;
			trace("起动监听");
		}
	}
	private function stopMe() {
		//停止监听
		if (_ifwork) {
			Mouse.removeListener(MObj);
			Key.removeListener(KObj);
			_ifwork = false;
			trace("停止监听");
		}
	}
}
